package org.grow.auth.service;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: xwg
 * @CreateDate: 2022/5/27
 */

@Service
public class PostAuthFailure implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception)
            throws IOException, ServletException {
        if (exception instanceof BadCredentialsException){
            response.setStatus(701);
            response.getWriter().println("登录失败："+exception.getMessage());
        }else if(exception instanceof DisabledException){
            response.setStatus(702);
            response.getWriter().println("登录失败:："+exception.getMessage());
        }

    }
}
